﻿using System.Collections;
using UnityEngine;

namespace FutureCorePlugin
{
    public class MainLauncher : MonoBehaviour
    {
        public static MainLauncher Instance { get; private set; }

        private void Awake()
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);

            Init();
            StartLauncher();
        }

        private void OnDestroy()
        {
            Instance = null;
        }

        private void Init()
        {
            new GameObject("[EngineEventSystem]").AddComponent<EngineEventSystem>();
            new GameObject("[PreUIHandler]").AddComponent<PreUIHandler>();
        }

        private void StartLauncher()
        {
            StartCoroutine(OnLauncher());
        }

        private IEnumerator OnLauncher()
        {
            float splashTime = 1.5f;
            PreGameSplashUI.Instance.Open(splashTime);
            yield return null;
            yield return new WaitForSeconds(splashTime);

            LauncherAssist();
            LauncherApp();
            LauncherSDK();
            LauncherAssembly();
        }

        private void LauncherAssist()
        {
            string path = "Preset/MainLauncher/Reporter";
            GameObject reporterGo = Instantiate(Resources.Load<GameObject>(path));
            reporterGo.transform.SetParent(transform, false);

            bool mode = LauncherConst.IsShowAssistDebug;
            reporterGo.SetActive(mode);
            if (mode)
            {
                Reporter reporter = reporterGo.GetComponent<Reporter>();
                reporter.numOfCircleToShow = LauncherConst.ShowLogsViewerCount;
                DebugUtil.Log("[MainLauncher]Init LogsViewer");
            }
        }

        private void LauncherApp()
        {
            App.Init();
            DebugUtil.LogFormat("[MainLauncher]Time:{0}", Time.unscaledTime);
        }

        private void LauncherSDK()
        {
            gameObject.AddComponent<SDKLauncher>();
        }

        private void LauncherAssembly()
        {
            gameObject.AddComponent<AssemblyLauncher>();
        }
    }
}